#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory 
#  to two tapes where the maximum tape file size is set to 1M
#  Note, this test simulates the tape filling and writing to
#   the next tape.
#
TestName="two-volume-changer"
JobName=twovolchanger
. scripts/functions

if test x$FORCE_CLOUD = xyes ; then
   echo "\n=== Test $TestName skipped not compatible with Cloud  ==="
   exit 0
fi

require_tape_drive
require_autochanger

scripts/cleanup
scripts/copy-2tape-confs
scripts/prepare-two-tapes
 
echo "${cwd}/build" >${cwd}/tmp/file-list

outf="tmp/sed_tmp"
echo "s%# Maximum File Size%  Maximum File Size%g" >${outf}
cp ${cwd}/bin/bacula-sd.conf ${cwd}/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bacula-sd.conf

change_jobname $JobName
start_test

# Write out bconsole commands
cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log1.out
label storage=tape volume=TestVolume001 slot=$SLOT1 pool=Default drive=$DRIVE1
label storage=tape volume=TestVolume002 slot=$SLOT2 pool=Default drive=$DRIVE1
update Volume=TestVolume002 MaxVolBytes=3000000 pool=Default drive=$DRIVE1
sql
select * from Storage;
select VolumeName,InChanger,Slot,StorageId from Media;

@#setdebug level=1000 client=$CLIENT 
setdebug level=150 storage=tape
run job=$JobName yes
wait
sql
select * from Storage;
select VolumeName,InChanger,Slot,StorageId from Media;

messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=tape
stop_bacula

$scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $SLOT2 ${TAPE_DRIVE} 0

cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@# 
@# now do a restore
@#
@$out tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select all storage=tape done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=tape
stop_bacula

check_two_logs
check_restore_diff
end_test
